//
//  CommonViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/1.
//

import UIKit
import SnapKit
class CommonViewController: UIViewController {
    var statusBarStyle:UIStatusBarStyle{
        set{
            style = newValue
            self.setNeedsStatusBarAppearanceUpdate()
        }
        get{
            return self.style
        }
    }
    private var style : UIStatusBarStyle = .default
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return self.style
    }
    var statusBarHidden:Bool{
        set{
            statusHidden = newValue
            self.setNeedsStatusBarAppearanceUpdate()
        }
        get{self.statusHidden}
    }
    private var statusHidden:Bool = false
    override var prefersStatusBarHidden: Bool{
        return statusHidden
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = BACKGROUND_COLOR
        self.navigationController?.navigationBar.isTranslucent = false
        if(self.navigationController != nil){
            if((self.navigationController?.viewControllers.count)! > 1){
                self.navigationItem.hidesBackButton = false
                let item = UIBarButtonItem.init(image: UIImage(named: "back")?.withRenderingMode(.alwaysTemplate), style: .plain, target: self, action: #selector(backBtnClick))
                item.tintColor = B000_COLOR
                self.navigationItem.leftBarButtonItem  = item
            }else{
                self.navigationItem.hidesBackButton = true
            }
        }
        if self.navigationController != nil {
            self.navigationController!.interactivePopGestureRecognizer!.delegate = self
        }
        
        NotificationMananger.addNotification(.NetWorkState, target: self, selector: #selector(netWorkChangeState(notification:)))
        
    }
    @objc func netWorkChangeState(notification:Notification){
        SystemLog("123456789")
    }
    @objc func backBtnClick(){
        self.navigationController?.popViewController(animated: true)
    }
 
    
    func pushViewController(_ vc:UIViewController,animated: Bool,hidesBottomBar:Bool = false) {
        
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: animated)
        if hidesBottomBar {
            self.hidesBottomBarWhenPushed = false
        }
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    init() {
        super.init(nibName: nil, bundle: nil)
        SystemLog("  init--\(self.description)--")
    }
    deinit {
        // 移除通知
        NotificationMananger.removeAllNotifocation(self)
        SystemLog("deinit--\(self.description)--")
    }

    override func didReceiveMemoryWarning() {
        SystemLog("MemoryWarning--\(self.description)--")
    }
}
extension CommonViewController:UIGestureRecognizerDelegate{
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.navigationController?.viewControllers.count == 1 ? false : true
    }
}
extension UIViewController{
    //点击空白,隐藏键盘
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
    
    @objc private func dismissKeyboard() {
        view.endEditing(true)
    }
    
}

